---@class Battle.BattleBase
---@field New fun():Battle.BattleBase
local BattleBase = Battle.CreateClass("BattleBase")

Battle.BattleBase = BattleBase
function BattleBase:Init(battleType)
    ---@type Battle.CBattle
    Battle.ClientBattle = self
    if self.isStart then
        self:Destroy()
    end
    self.battleType = battleType
    self:Create()
end
function BattleBase:Leave()
    Battle.GlobalEventSystem.BroadCast(Battle.Event.BattleLeave)
    self:Destroy()
end

function BattleBase:Create()
    self.isStart = true
    self:StartUpdateTime()
    self.updateCallbackList = {}
    self.lateUpdateCallbackList = {}
    self.battleTime = 0
    self.battleStartTime = os.time()
    self.frameCount = 0
    self.deltaTime = 0
    self.countdownTime = 0
    self.battleResult = 0
    self.battleStage = 0
    self.isStartCountdown = false
    self.managerList = {}
    Battle.IDGenerator.Reset()
    self:OnCreate()
end
function BattleBase:Destroy()
    if not self.isStart then
        return
    end
    self.isStart = false
    self:OnDestroy()
    for k,v in pairs(self.managerList) do
        v.OnDestroy()
    end
    self:EndUpdateTime()
    self.updateCallbackList = nil
    self.lateUpdateCallbackList = nil
    Battle.ClientBattle = nil
end

function BattleBase:AddManager(manager)
    if manager == nil then return end
    manager:OnCreate()
    table.insert(self.managerList,manager)
end

function BattleBase:RegisterUpdateCallback(callback,tb)
    for k,v in pairs(self.updateCallbackList) do
        if v.callback == callback and v.tb == tb then
            return
        end
    end
    table.insert(self.updateCallbackList,{callback = callback,tb = tb})
end
function BattleBase:RegisterLateUpdateCallback(callback,tb)
    for k,v in pairs(self.lateUpdateCallbackList) do
        if v.callback == callback and v.tb == tb then
            return
        end
    end
    table.insert(self.lateUpdateCallbackList,{callback = callback,tb = tb})
end
function BattleBase:IsBattleFinished()
    return self.battleStage >= Battle.BattleStage.Finished
end
function BattleBase:SetBattleStage(stage)
    self.battleStage = stage
    Battle.GlobalEventSystem.BroadCast(Battle.Event.BattleStageChanged,Battle.EventArgs.BattleStageChangeArgs:Create(self.battleStage))
end
function BattleBase:Update()
    if self.battleStage == Battle.BattleStage.Result then
        return
    end
    self.frameCount = self.frameCount + 1
    local dt = Time.deltaTime
    self.deltaTime = dt
    self.battleTime = os.time() - self.battleStartTime
    if self.isStartCountdown then
        self.countdownTime = self.countdownTime - dt
        if self.countdownTime <= 0 then
            self.isStartCountdown = false
            self.countdownTime = 0
            self:CountdownFinished()
        end
    end
    --update
    for k,v in pairs(self.updateCallbackList) do
        if v.tb then
            v.callback(v.tb,dt)
        else
            v.callback(dt)
        end
    end
    --late update
    for k,v in pairs(self.lateUpdateCallbackList) do
        if v.tb then
            v.callback(v.tb,dt)
        else
            v.callback(dt)
        end
    end
end
function BattleBase:StartCountdown(time)
    self.countdownTime = time
    self.isStartCountdown = true
    Battle.GlobalEventSystem.BroadCast(Battle.Event.BattleTimeInit)
end

function BattleBase:StartUpdateTime()
    self:EndUpdateTime()
    self.updateTimerId = UTimer.FrameCall(self.Update,self,-1)
end
function BattleBase:EndUpdateTime()
    if self.updateTimerId then
        UTimer.Remove(self.updateTimerId)
        self.updateTimerId = nil
    end
end
function BattleBase:OnCreate()

end
function BattleBase:OnDestroy()

end
function BattleBase:SceneLoadDone()

end
function BattleBase:SetActive(isActive)

end

function BattleBase:CountdownFinished()

end